package com.mall.dubbo.api;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mall.dubbo.mapper.OmsOrderMapper;
import com.mall.dubbo.mapper.OmsOrderOperateHistoryMapper;
import com.mall.model.domain.OmsOrder;
import com.mall.model.dto.OmsOrderDeliveryDto;
import com.mall.model.dto.OmsOrderDto;
import com.mall.model.enums.OrderStatus;
import org.apache.commons.lang.StringUtils;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * @author xyiaao
 * @description 针对表【oms_order(订单表)】的数据库操作Api实现
 * @createDate 2022-03-29 19:46:09
 */
@DubboService
public class OmsOrderApiImpl extends ServiceImpl<OmsOrderMapper, OmsOrder>
        implements OmsOrderApi {

    @Autowired
    private OmsOrderMapper omsOrderMapper;


    @Autowired
    private OmsOrderOperateHistoryMapper omsOrderOperateHistoryMapper;


    @Autowired
    private OmsOrderOperateHistoryApi omsOrderOperateHistoryApi;

    @Override
    public Page<OmsOrder> pageByOrderDto(OmsOrderDto omsOrderDto) {

        Page<OmsOrder> page = new Page<>(omsOrderDto.getPageNum(), omsOrderDto.getPageSize());

        LambdaQueryWrapper<OmsOrder> queryWrapper = new LambdaQueryWrapper<>();
        // 订单提交日期
        queryWrapper.apply(StringUtils.isNotEmpty(omsOrderDto.getCreateTime()), "DATE_FORMAT(create_time, '%Y-%m-%d') = {0}", omsOrderDto.getCreateTime())
                // 订单编号
                .like(StringUtils.isNotEmpty(omsOrderDto.getOrderSn()), OmsOrder::getOrderSn, omsOrderDto.getOrderSn())
                // 订单类型
                .eq(omsOrderDto.getOrderType() != null, OmsOrder::getOrderType, omsOrderDto.getOrderType())
                // 收获人姓名
                .like(StringUtils.isNotEmpty(omsOrderDto.getReceiverKeyword()), OmsOrder::getReceiverName, omsOrderDto.getReceiverKeyword())
                // 收获人号码
                .or().like(StringUtils.isNotEmpty(omsOrderDto.getReceiverKeyword()), OmsOrder::getReceiverPhone, omsOrderDto.getReceiverKeyword())
                // 订单来源
                .eq(omsOrderDto.getSourceType() != null, OmsOrder::getSourceType, omsOrderDto.getSourceType())
                // 订单状态
                .eq(omsOrderDto.getStatus() != null, OmsOrder::getStatus, omsOrderDto.getStatus())
                // 按创建时间倒序
                .orderByDesc(OmsOrder::getCreateTime);

        return omsOrderMapper.selectPage(page, queryWrapper);
    }

    @Override
    public void updateClose(Long userId, List<Long> ids, int status, String note) {

        // 先将订单状态更新
        LambdaUpdateWrapper<OmsOrder> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.set(OmsOrder::getStatus, status);
        updateWrapper.in(OmsOrder::getId, ids);
        this.update(updateWrapper);

        // 保存订单操作记录
        omsOrderOperateHistoryApi.insertOmsOrderOperateHistory(userId, ids, status, note);
    }


    @Override
    public void updateClose(Long userId, Long id, int status, String note) {

        // 先将订单状态更新
        LambdaUpdateWrapper<OmsOrder> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.set(OmsOrder::getStatus, status);
        updateWrapper.eq(OmsOrder::getId, id);
        this.update(updateWrapper);

        // 保存订单操作记录
        omsOrderOperateHistoryApi.insertOmsOrderOperateHistory(userId, id, status, note);
    }


    @Override
    public void updateDelivery(Long userId, List<OmsOrderDeliveryDto> deliveryParamList, int status) {

        // 更新订单信息
        for (OmsOrderDeliveryDto omsOrderDeliveryDto : deliveryParamList) {
            LambdaUpdateWrapper<OmsOrder> updateWrapper = new LambdaUpdateWrapper<>();
            updateWrapper.set(OmsOrder::getDeliveryCompany, omsOrderDeliveryDto.getDeliveryCompany());
            updateWrapper.set(OmsOrder::getDeliverySn, omsOrderDeliveryDto.getDeliverySn());
            updateWrapper.set(OmsOrder::getStatus, status);
            updateWrapper.eq(OmsOrder::getId, omsOrderDeliveryDto.getOrderId());
            this.update(updateWrapper);
        }

        // 保存订单操作记录
        List<Long> orderIds = CollUtil.getFieldValues(deliveryParamList, "orderId", Long.class);
        omsOrderOperateHistoryApi.insertOmsOrderOperateHistory(userId, orderIds, status, OrderStatus.SHIPPED.getNote());
    }

}




